from django.http import JsonResponse
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import GenericViewSet
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from django.core.paginator import Paginator
from core.decorator import monitor, has_permi
from core.models import ContentLog
from core.serializers import ContentLogSerializer
# form core.serializers import person_zhihuInfo
from core.utils import get_label_tree

@monitor
class ZhiHuView(GenericViewSet):

    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    # @has_permi('system:menu:list')
    def list(self, request):
        # menu_name = request.query_params.get('menu_name')
        # status = request.query_params.get('status')

        # query_condition = {}
        # if menu_name:
        #     query_condition['menu_name'] = menu_name
        # if status:
        #     query_condition['status'] = status

        # menus = SysMenu.objects.filter(**query_condition)
        # serializer = SysMenuSerializer(menus, many=True)
        # for menu in serializer.data:
        #     menu['children'] = []
        # print('测试abc')
        # res = {
        #     'code': 200,
        #     'msg': 'okkkkk',
        # }
        # return JsonResponse(res)
       

        page_num = request.query_params.get('page_num')
        page_size = request.query_params.get('page_size')
        title = request.query_params.get('content_title')
        operator = request.query_params.get('content_message')
        business_type = request.query_params.get('content_trash')
        
        query_condition = {}
        if title:
            query_condition['content_title'] = title

        if operator:
            query_condition['content_message'] = operator

        if business_type:
            query_condition['content_trash'] = business_type

        

        operation_logs = ContentLog.objects.filter(**query_condition).order_by('-create_time')
        paginator = Paginator(operation_logs, page_size)
        operation_logs = paginator.get_page(page_num)
        serializer = ContentLogSerializer(operation_logs, many=True)
        res = {
            'code': 200,
            'msg': 'ok',
            'total': paginator.count,
            'rows': serializer.data
        }
        return JsonResponse(res)

    # @has_permi('system:menu:add')
    def create(self, request):
        """新增菜单"""
        # request.data['create_by'] = request.user.username
        serializer = ContentLogSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        res = {
            'code': 200,
            'msg': 'ok'
        }
        return JsonResponse(res)

